// Rupert Michiels

//Matthew - i have changed setcoeffienct and getcoefficient to setCoeffienct and getCoeffienct
//so changed those names here

import gates.QuantumGate;

import java.math.*;

import master.*;


public class Cnot implements QuantumGate {

	private int dim;
	private int N;
	
	public Cnot(int dim) {
		this.dim = dim;
		N = dim; // Oliver(16/02/13): made a change here as it did not need to square the dimension again
		
		
	}
	
	

	public QuantumRegister actOnReg(QuantumRegister reg, int cbit, int abit) {
		
		// Oliver(16/02/13): Changed the return type as it was not returning anything so I could use it to act on registers
		
		if(cbit<= abit || (cbit==dim)) ; //displlay error message ,Online forward-controlling for now
		int d1 = (dim - cbit)*2;
		int d2 = (dim-abit)*2;
		if(d1==0) d1=1;
		if(d2==0) d2=1;
		ComplexNumber[] mem = new ComplexNumber[d2];
		int no = (d1-1);
		while(no<N-1) {
			for(int n = no+1;n<=(no+d2);n++) {
				mem[n] = reg.getCoefficient(n);
				reg.setCoefficient(reg.getCoefficient(n+d2), n);
				
			}
			no = no+d2;
			for(int n = no+1;n<=(no+d2);n++) {
				reg.setCoefficient(mem[n-d2], n);
			}
			no = no+d2;
			no = no+d1;
		}
		return reg;
	}
			
	
	
	public boolean checkDim(QuantumRegister reg) throws IllegalArgumentException {
		if(reg.getDimension() == dim) return true ;
		else return false;
	}


/*
	@Override
	public QuantumRegister actOnReg(QuantumRegister register) {
		// TODO Auto-generated method stub
		return null;
	}
*/


	@Override
	public QuantumRegister actOnReg(QuantumRegister register, int bit1) {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public QuantumRegister actOnReg(QuantumRegister register, int bit1,
			int bit2, int bit3) {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public QuantumGate multiply(QuantumGate gate) {
		// TODO Auto-generated method stub
		return null;
	}
}